package com.pick.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pick.admin.entity.SysUser;
import com.pick.admin.service.SysUserService;
import com.pick.base.annotation.OperationLog;
import com.pick.base.common.PageQuery;
import com.pick.base.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @Operation(summary = "分页查询用户列表")
    @SaCheckPermission("system:user:list")
    @GetMapping("/page")
    public Result<IPage<SysUser>> pageUsers(PageQuery pageQuery) {
        IPage<SysUser> page = userService.pageUsers(pageQuery);
        return Result.success(page);
    }

    @Operation(summary = "根据ID查询用户")
    @SaCheckPermission("system:user:query")
    @GetMapping("/{id}")
    public Result<SysUser> getUser(@PathVariable Long id) {
        SysUser user = userService.getById(id);
        // 清空密码字段
        if (user != null) {
            user.setPassword(null);
        }
        return Result.success(user);
    }

    @Operation(summary = "创建用户")
    @OperationLog(module = "用户管理", type = "新增", description = "创建用户")
    @SaCheckPermission("system:user:add")
    @PostMapping
    public Result<String> createUser(@RequestBody SysUser user) {
        boolean success = userService.createUser(user);
        return success ? Result.success("创建成功") : Result.error("创建失败");
    }

    @Operation(summary = "更新用户")
    @OperationLog(module = "用户管理", type = "修改", description = "更新用户")
    @SaCheckPermission("system:user:edit")
    @PutMapping
    public Result<String> updateUser(@RequestBody SysUser user) {
        boolean success = userService.updateUser(user);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Operation(summary = "删除用户")
    @OperationLog(module = "用户管理", type = "删除", description = "删除用户")
    @SaCheckPermission("system:user:remove")
    @DeleteMapping("/{id}")
    public Result<String> deleteUser(@PathVariable Long id) {
        boolean success = userService.deleteUser(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Operation(summary = "重置密码")
    @OperationLog(module = "用户管理", type = "重置密码", description = "重置用户密码")
    @SaCheckPermission("system:user:resetPwd")
    @PutMapping("/resetPassword")
    public Result<String> resetPassword(@RequestBody ResetPasswordRequest request) {
        boolean success = userService.resetPassword(request.getUserId(), request.getNewPassword());
        return success ? Result.success("重置成功") : Result.error("重置失败");
    }

    @Operation(summary = "修改密码")
    @OperationLog(module = "用户管理", type = "修改密码", description = "修改用户密码")
    @PutMapping("/changePassword")
    public Result<String> changePassword(@RequestBody ChangePasswordRequest request) {
        boolean success = userService.changePassword(request.getUserId(), 
                request.getOldPassword(), request.getNewPassword());
        return success ? Result.success("修改成功") : Result.error("修改失败");
    }

    @Operation(summary = "分配角色")
    @OperationLog(module = "用户管理", type = "分配角色", description = "为用户分配角色")
    @SaCheckPermission("system:user:role")
    @PutMapping("/assignRoles")
    public Result<String> assignRoles(@RequestBody AssignRolesRequest request) {
        boolean success = userService.assignRoles(request.getUserId(), request.getRoleIds());
        return success ? Result.success("分配成功") : Result.error("分配失败");
    }

    /**
     * 重置密码请求参数
     */
    @Data
    public static class ResetPasswordRequest {
        private Long userId;
        private String newPassword;
    }

    /**
     * 修改密码请求参数
     */
    @Data
    public static class ChangePasswordRequest {
        private Long userId;
        private String oldPassword;
        private String newPassword;
    }

    /**
     * 分配角色请求参数
     */
    @Data
    public static class AssignRolesRequest {
        private Long userId;
        private List<Long> roleIds;
    }
}